package com.shengdong.water.code.base
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView

/**
 * 多类型RecyclerView的Adapter封装
 */
abstract class MultiTypeAdapter<T> : RecyclerView.Adapter<MultiTypeAdapter.MultiTypeViewHolder>() {

    // 数据列表
    private val items = mutableListOf<T>()

    // 点击事件监听器
    private var itemClickListener: ((T, Int) -> Unit)? = null

    // 获取视图类型的抽象方法
    abstract fun getItemViewType(position: Int, item: T): Int

    // 根据视图类型获取布局ID的抽象方法
    abstract fun getLayoutId(viewType: Int): Int

    // 绑定数据的抽象方法，强制子类实现具体绑定逻辑
    abstract fun bindData(binding: ViewDataBinding, item: T, position: Int, viewType: Int)

    // 设置点击事件监听器
    fun setOnItemClickListener(listener: (T, Int) -> Unit) {
        this.itemClickListener = listener
    }

    // 更新数据列表
    fun setItems(newItems: List<T>) {
        val diffCallback = object : DiffUtil.Callback() {
            override fun getOldListSize() = items.size
            override fun getNewListSize() = newItems.size

            override fun areItemsTheSame(oldPos: Int, newPos: Int): Boolean {
                return areItemsTheSame(items[oldPos], newItems[newPos])
            }

            override fun areContentsTheSame(oldPos: Int, newPos: Int): Boolean {
                return areContentsTheSame(items[oldPos], newItems[newPos])
            }
        }

        val diffResult = DiffUtil.calculateDiff(diffCallback)
        items.clear()
        items.addAll(newItems)
        diffResult.dispatchUpdatesTo(this)
    }

    // 判断两个item是否是同一个，默认实现，子类可重写
    open fun areItemsTheSame(oldItem: T, newItem: T): Boolean {
        return oldItem == newItem
    }

    // 判断两个item内容是否相同，默认实现，子类可重写
    open fun areContentsTheSame(oldItem: T, newItem: T): Boolean {
        return oldItem == newItem
    }

    // 获取指定位置的数据
    fun getItem(position: Int): T = items[position]

    // 获取当前数据列表
    fun getItems(): List<T> = items

    override fun getItemViewType(position: Int): Int {
        return getItemViewType(position, items[position])
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MultiTypeViewHolder {
        val binding = DataBindingUtil.inflate<ViewDataBinding>(
            LayoutInflater.from(parent.context),
            getLayoutId(viewType),
            parent,
            false
        )
        return MultiTypeViewHolder(binding)
    }

    override fun onBindViewHolder(holder: MultiTypeViewHolder, position: Int) {
        val item = items[position]
        val viewType = getItemViewType(position)

        bindData(holder.binding, item, position, viewType)

        // 设置点击事件
        holder.binding.root.setOnClickListener {
            itemClickListener?.invoke(item, position)
        }

        // 确保绑定会立即执行
        holder.binding.executePendingBindings()
    }

    override fun getItemCount(): Int = items.size

    // 多类型ViewHolder
    class MultiTypeViewHolder(val binding: ViewDataBinding) :
        RecyclerView.ViewHolder(binding.root)
}